package org.cliff.message.platform.management.exception;

import org.cliff.message.platform.core.exception.MPBusinessException;
import org.cliff.message.platform.management.domain.MPR;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author 刘文
 * @date 2023/10/30
 */
@RestControllerAdvice(basePackages = "org.cliff.message.platform")
@Order(Ordered.HIGHEST_PRECEDENCE)
@Slf4j
public class MPExceptionHandler {

    @ExceptionHandler(MPBusinessException.class)
    public MPR<Void> handleMPBusinessException(MPBusinessException exception) {
        log.warn("Message platform business aborted for: {}", exception.getMessage());
        if (exception.getErrorCode() == null) {
            return MPR.failed(exception.getMessage());
        }
        return MPR.failed(exception.getErrorCode(), exception.getMessage());
    }
}
